import Card from './card'
import * as _ from 'lodash'
import resource from './resource'
const log = console.log

export class cardsShower{
    anchor:cc.Node
    cards: Array<Card> = []
    cardsinfo:Array<object>
    scale = 0.5
    constructor(position:cc.Vec2, scale=0.5){
        this.anchor = new cc.Node()
        cc.find("Canvas").addChild(this.anchor)
        this.anchor.setPosition(position)
        this.scale=scale
    }
    newCard(cardinfo, idx):Card{
        var card:Card=cc.instantiate(cc.find("Canvas").getComponent(resource).card).getComponent(Card);
        card.InitCard(cardinfo)
        card.node.setScale(this.scale)
        this.anchor.addChild(card.node)
        return card
    }
    centifyCard(){
        if(!this.cards.length) return
        var cardViewWidth=this.cards[0].node.width/2*this.cards[0].node.scaleX
        var cardBias=-this.cards.length/2*cardViewWidth;
        this.cards.map(card=>{
            card.node.setPosition(cc.v2(cardBias=cardBias+cardViewWidth,0))
        })
    }
    display(cardsinfo){
        this.cardsinfo = cardsinfo
        this.cards = []
        this.anchor.removeAllChildren()
        cardsinfo.map((info,idx)=>{
            this.cards.push(this.newCard(info,idx))
        })
        this.centifyCard()
    }
    destroy(){
        this.anchor.destroy()
        this.cards.map(card=>card.destroy())
    }   
}


export class handCardManager extends cardsShower{
    private selectStartIdx:number = null
    chooseIdxs: Array<number> = []
    private selectCnt={
        Value:null, onChange:null,
        set value(val){
            if(this.onChange && val!=this.Value) this.onChange(val,this.Value);
            this.Value=val;
        },
        get value(){ return this.Value }
    }
    constructor(){
        super(cc.v2(0, 70-cc.view.getVisibleSize().height/2), 1)
        this.selectCnt.onChange=(value,origin)=>{
            var inc=value-origin>0 == value>0 && value!=0
            if(value==null || origin==null) return//选择完毕
            this.operateCards(inc,-1,inc,origin,value)//选择减少时 少减一张
        }
    }
    newCard(cardinfo, idx){
        var halfScreenWidth=cc.view.getVisibleSize().width/2
        var card:Card=super.newCard(cardinfo, 1)

        card.Idx =idx
        card.info = cardinfo

        card.node.on(cc.Node.EventType.TOUCH_START ,( )=>{ 
            card.hover(true); this.selectStartIdx=card.Idx;  
        })
        card.node.on(cc.Node.EventType.TOUCH_END   ,( )=>{ 
            card.hover(false);  
            card.choose()
            this.selectCnt.value=null
            this.updateChooseIdxs()
        })
        card.node.on(cc.Node.EventType.TOUCH_MOVE  ,(e:cc.Touch)=>{ 
            this.selectCnt.value=Math.floor((e.getLocation().x-card.node.x-halfScreenWidth)/(card.node.width/2))+1
         })
        card.node.on(cc.Node.EventType.TOUCH_CANCEL,()=>{
            if(this.selectCnt.value==0) return;
            this.operateCards(false,null)
            this.selectCnt.value=null
            this.updateChooseIdxs()
        })
        return card
    }
    updateChooseIdxs(){
        this.chooseIdxs=[]
        for(var i=0;i<this.cards.length;i++)
            if(this.cards[i].getChoose())
                this.chooseIdxs.push(i)
        this.chooseIdxs.sort((a,b)=>{return b-a})
    }
    operateCards(hover,choose,inc=true,fromCnt=0, toCnt=this.selectCnt.value){
        var start = this.selectStartIdx + fromCnt 
        var end   = this.selectStartIdx + toCnt
        var from  = Math.max(0, Math.min(start,end)) + (inc?0:(fromCnt>0?+1:-1))
        var to    = Math.min(this.cards.length-1, Math.max(start,end))
        for(var i=from;i<=to;i++) {
            if(i<this.cards.length && i>=0){
                if(choose!=-1) this.cards[i].choose(choose)
                if(hover !=-1) this.cards[i].hover(hover)
            }
        }
    }
    playOut(){
        this.chooseIdxs.map(idx=>{
            this.anchor.removeChild(this.cards[idx].node)
            _.pullAt(this.cards, idx)
        })
        this.cards.map((card, idx)=>{
            card.Idx = idx
        })
        this.centifyCard()
    }
    getPlayOut():Array<object>{
        var outcardsInfo: Array<object>=[]
        this.chooseIdxs.map(idx=>{
            outcardsInfo.push(this.cards[idx].info)
        })
        return outcardsInfo
    }
}